﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Vanglen.BillBox
{
    public partial class Detail : PhoneApplicationPage
    {
        public Detail()
        {
            InitializeComponent();
        }

        private void ListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tbDate_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BindData()
        {
            using (SQLiteHelper helper = new SQLiteHelper())
            {
                string strY = this.tbDate.Value.Value.Year.ToString();
                string strD = this.tbDate.Value.Value.Month.ToString("00");

                //列表
                string sql = String.Format("select * from bills where date between '{0}-{1}-01' and '{0}-{1}-31';",
                    strY, strD);
                lbDetail.ItemsSource = helper.ExecuteQuery<Bills>(sql).ToList();

                //收入
                string sql_In = String.Format("select sum(cost) as costSum,* from bills where items in ('[收入]工资','[收入]其它') and date between '{0}-{1}-01' and '{0}-{1}-31';",
                    strY, strD);
                List<Bills> listIn = helper.ExecuteQuery<Bills>(sql_In).ToList();

                //支出
                string sql_Out = String.Format("select sum(cost) as costSum,* from bills where items in ('[支出]生活用品','[支出]水电煤气费','[支出]汽油费','[支出]其它') and date between '{0}-{1}-01' and '{0}-{1}-31';",
                    strY, strD);
                List<Bills> listOut = helper.ExecuteQuery<Bills>(sql_Out).ToList();

                string strIn = listIn[0].cost ?? "0";
                string strOut = listOut[0].cost ?? "0";

                tbTotal.Text = String.Format("当月收入：{0}，支出：{1}，小计：{2}", strIn, strOut, Convert.ToInt32(strIn) - Convert.ToInt32(strOut));
            }
        }

        private void DelData(int id)
        {
            using (SQLiteHelper helper = new SQLiteHelper())
            {
                string sql = String.Format("delete from bills where id={0};", id);
                int result = helper.ExecuteNonQuery(sql);
                if (result > 0)
                    MessageBox.Show("删除成功！");
                else
                    MessageBox.Show("删除失败！");
                BindData();
            }
        }

        private void DelItem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string header = (sender as MenuItem).Header.ToString();

                ListBoxItem selectedListBoxItem = this.lbDetail.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
                if (selectedListBoxItem == null)
                {
                    return;
                }

                if (header == "删除")
                {
                    DelData((selectedListBoxItem.Content as Bills).id);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}